﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.StorageClient;

namespace Microsoft.WindowsAzure.Search
{
    public class IndexWriter
    {
        public static void Write(Dictionary<String, Entry>dictionary, CloudBlobContainer indexContainer)
        {
            var result = from pair in dictionary orderby pair.Key select pair;

            String index = "";

            int k = 0;
            foreach (KeyValuePair<String, Entry> pair in result)
            {
                if (k != 0) index += "\n";
                Entry entry = pair.Value;
                index = index + pair.Key + "," + entry.DocFreq;
                foreach (Posting positing in entry.Posting_list)
                {
                    index = index + "," + positing.DocID + "," + positing.TermFreq;
                }
                k++;
            }

            CloudBlob indexBlob = indexContainer.GetBlobReference("index");
            indexBlob.UploadText(index);
        }
    }
}